package com.springcloud.sc.ucpv5.client.application.message.type.shortType;

import java.nio.ByteBuffer;

import com.springcloud.sc.ucpv5.client.application.message.type.NumberWrap;

/**
 * 无符号短整型及有符号短整型公用函数
 * 
 */
public class ShortWrapPublic extends NumberWrap {
    public static final int len = 2;

    /**
     * 组包短整型，短整型转换成指定字节放入缓存存储
     * 
     * @param buffer
     * @param value
     * @param key
     *            包含所有该类型所需的属性条件
     */
    protected static byte[] packUnShortPublic(short value, int length, boolean isOrder) {
	ByteBuffer tempBuffer = allocTempBuffer(length);
	tempBuffer.putShort(value);
	tempBuffer = makeByteOrder(tempBuffer, isOrder);

	return tempBuffer.array();
    }

    /**
     * 解包无符号短整型,从缓存里取出两个字节的数据，如果指定长度大于2，则抛弃多余长度的字节
     * 
     * @param buffer
     * @param key
     *            包含所有该类型所需的属性条件
     */
    protected static ByteBuffer unpackShortPublic(ByteBuffer buffer, int length, boolean isOrder) {
	ByteBuffer tempBuffer = ByteBuffer.allocate(len);
	tempBuffer.putShort(buffer.getShort());

	tempBuffer = makeByteOrder(tempBuffer, isOrder);

	unpackKeyByRestLength(buffer, length, len);

	return tempBuffer;
    }

}
